package com.movie2.service.impl;


import cn.hutool.core.util.NumberUtil;
import com.movie2.config.MyConstants;
import com.movie2.mapper.FilmMapper;
import com.movie2.model.DTO.Test2Dto;
import com.movie2.model.entity.Film;
import com.movie2.model.entity.Test;
import com.movie2.model.entity.Test1;
import com.movie2.model.entity.Test2;
import com.movie2.model.request.TestRequest;
import com.movie2.model.vo.Test1Vo;
import com.movie2.model.vo.Test1_1Vo;
import com.movie2.model.vo.Test2Vo;
import com.movie2.service.FilmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * FilmService业务逻辑操作接口类的实现类
 **/

@Service
@Transactional
@Slf4j
public class FilmServiceImpl implements FilmService {

    @Autowired
    FilmMapper filmMapper;

    @Override
    public List<Film> getFilmsHot(int hot) {
        log.info("获得热门电影Film信息:getFilmsHot(int hot) ");
        List<Film> filmsHot = filmMapper.getFilmsHot(hot);
        for (Film film : filmsHot) {
            film.setCover(MyConstants.MY_URL + film.getCover());
        }
        return filmsHot;
    }

    //查询（根据主键ID查询）
    @Override
    public Film selectByPrimaryKey(Integer id) {
        log.info("通过主键查询Film信息:selectByPrimaryKey() ");
        return filmMapper.selectByPrimaryKey(id);
    }
    
    @Override
    //查询（获得数据表中的数量）
    public int selectFilmCount() {
        log.info("获得Film数量信息:selectFilmCount() ");
        return filmMapper.selectFilmCount();
    }

    //查询全部信息
    @Override
    public List<Film> selectAllFilms() {
        log.info("获得全部Film信息:selectAllFilms() ");
        List<Film> films = filmMapper.selectAllFilms();
        for (Film film : films) {
            film.setCover(MyConstants.MY_URL + film.getCover());
        }
        return films;
        // return filmMapper.selectAllFilms();
    }
    @Override
    public List<Film> selectAllFilms(String type, String area) {
        log.info("获得全部Film信息:selectAllFilms(String type,String area) ");
        Map<String,Object> map=new HashMap<>();
        map.put("type",type);
        map.put("area",area);
        List<Film> films = filmMapper.selectFilmsCondition(map);
        for (Film film : films) {
            film.setCover(MyConstants.MY_URL + film.getCover());
        }
        return films;
        // return filmMapper.selectAllFilms();
    }

    //查询（根据其他条件查询需要的结果）

    @Override
    public int selectFilmsCountByCondition(Map<String, Object> map) {
        log.info("根据其他条件查询需要的Film信息:selectFilmsCountByCondition() ");
        return filmMapper.selectFilmsCountByCondition(map);
    }

    //查询（通过Map条件，获得所有符合的信息）

    @Override
    public List<Film> selectFilmsCondition(Map<String, Object> map) {
        log.info("根据其他条件查询需要的Film信息:selectFilmsCountByCondition() ");
        return filmMapper.selectFilmsCondition(map);
    }

    //查询（通过条件进行分页查询）

    @Override
    public List<Film> selectFilmsPerPageByCondition(Map<String, Object> map) {
        log.info("通过条件进行分页查询Film信息:selectFilmsPerPageByCondition() ");
        return filmMapper.selectFilmsPerPageByCondition(map);
    }

    //条件查询
    @Override
    public List<Film> selectFilmsByCondition(Film record) {
        log.info("通过传入的参数条件Film信息进行查询:selectFilmsByCondition () ");
        return filmMapper.selectFilmsByCondition(record);
    }

    //删除（条件删除）
    @Override
    public int deleteByCondition(Film record) {
        log.info("根据传入的Film信息，条件删除:deleteByCondition() ");
        return filmMapper.deleteByCondition(record);
    }

    //删除（根据主键ID删除）
    @Override
    public int deleteByPrimaryKey(Integer id) {
        log.info("主键ID删除Film信息:deleteByPrimaryKey() ");
        return filmMapper.deleteByPrimaryKey(id);
    }

    //添加
    @Override
    public int insert(Film record) {
        log.info("新增Film信息:insert() ");
        return filmMapper.insert(record);
    }

    //添加 （匹配有值的字段）
    @Override
    public int insertSelective(Film record) {
        log.info("新增（匹配有值的字段）Film信息:insertSelective() ");
        return filmMapper.insertSelective(record);
    }

    //修改 （匹配有值的字段）
    @Override
    public int updateByPrimaryKeySelective(Film record) {
        log.info("修改（匹配有值的字段）Film信息:updateByPrimaryKeySelective() ");
        return filmMapper.updateByPrimaryKeySelective(record);
    }

    //修改（根据主键ID修改）
    @Override
    public int updateByPrimaryKey(Film record) {
        log.info("修改Film信息:updateByPrimaryKey() ");
        return filmMapper.updateByPrimaryKey(record);
    }
    
    
    @Override
    public List<Test1Vo> test4()  {
        return filmMapper.test4();
    }
    @Override
    public List<Test2Vo> test5()  {
        return filmMapper.test5();
    }
    @Override
    public void test6(List<Test2> test2List){
        filmMapper.test6(test2List);
    }
    @Override
    public void test7(Test test)  {
        log.info("test:{}",test);
        int i=filmMapper.test7(test);
        log.info("result:{}",i);
        log.info("=====getId:{}",test.getId());
    }
    @Override
    public void test8(TestRequest testRequest)  {
        log.info("testRequest:{}",testRequest);
        Test1 test1=new Test1();
        BeanUtils.copyProperties(testRequest,test1);
        // if (testRequest.getFlag()){
        //     test1.setFlag(1);
        // }else {
        //     test1.setFlag(0);
        // }
        log.info("test1:{}",test1);
        List<Test2> test2List= new ArrayList<>();
        List<Test2Dto> list1=testRequest.getDtoList();
        // log.info("list1:{}",list1);
        for (Test2Dto test2Dto : list1) {
            Test2 test2=new Test2();
            BeanUtils.copyProperties(test2Dto,test2);
            test2List.add(test2);
        }
        log.info("list:{}",test2List);
        filmMapper.test8_1(test1);
        filmMapper.test8_2(test2List);
    }
    @Override
    public void test9(List<Integer> list){
        filmMapper.test9(list);
    }
    
    @Override
    public void test10(double num1,double num2) {
        // Map<String,Object> map=filmMapper.test10(id);
        // log.info("map:{}",map);
        // log.info("FLAG:{}",map.get("FLAG").getClass().getName());
        // log.info("test:{}",Integer.parseInt(map.get("FLAG").toString())==0);
        BigDecimal bd1=BigDecimal.valueOf(num1);
        BigDecimal bd2=BigDecimal.valueOf(num2);
        double num3=bd1.divide(bd2,7,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100.00)).doubleValue();
        log.info("处理前double：{}",num3);
        double num4=bd1.divide(bd2,2,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100.00)).doubleValue();
        log.info("处理后double：{}",num4);
        int num5 = bd1.divide(bd2, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100.00)).intValue();
        log.info("处理后int：{}",num5);
        
    }
    
    @Override
    public void test11(List<Test> testList) {
        log.info("testList:{}",testList);
        filmMapper.test11(testList);
    }
    
    @Override
    public void test12() {
        List<Test1> list=filmMapper.test12();
        log.info("test1:{}",list);
    }
    
    @Override
    public void test13(List<Long> testList) {
        filmMapper.test13(testList);
    }
    
    @Override
    public Test1_1Vo test14(List<Integer> idList) {
        log.info("idList:{}",idList);
        return filmMapper.test14(idList);
    }
    
    @Override
    public void test15(Long num) {
        filmMapper.updateOne(num);
    }
    
    
}